package com.invest;

import org.apache.http.Header;
import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

public class Main1221 {
    public static void main(String[] args) throws IOException {
        String coo = getcoo();
        System.out.println(coo);

        try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
            // 创建HttpGet请求
            HttpGet request = new HttpGet("https://www.jisilu.cn/webapi/cb/list/");
            request.setHeader("Accept", "application/json, text/plain, */*");
            request.setHeader("Accept-Encoding", "gzip, deflate, br, zstd");
            request.setHeader("Accept-Language", "en,en-GB;q=0.9,en-US;q=0.8,zh-CN;q=0.7,zh;q=0.6");
            request.setHeader("Columns", "1,70,2,3,5,6,11,12,14,15,16,29,30,32,34,44,46,47,50,52,53,54,56,57,58,59,60,62,63,67");
            request.setHeader("Cookie", coo);
            request.setHeader("If-Modified-Since", "Tue, 19 Jan 2025 08:03:36 GMT");
            request.setHeader("Init", "1");
            request.setHeader("Priority", "u=1, i");
            request.setHeader("Referer", "https://www.jisilu.cn/web/data/cb/list");
            request.setHeader("Sec-Ch-Ua", "\"Google Chrome\";v=\"131\", \"Chromium\";v=\"131\", \"Not_A Brand\";v=\"24\"");
            request.setHeader("Sec-Ch-Ua-Mobile", "?0");
            request.setHeader("Sec-Ch-Ua-Platform", "\"Windows\"");
            request.setHeader("Sec-Fetch-Dest", "empty");
            request.setHeader("Sec-Fetch-Mode", "cors");
            request.setHeader("Sec-Fetch-Site", "same-origin");
            request.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36");

            // 发送请求
            try (CloseableHttpResponse response = httpClient.execute(request)) {
                // 获取响应状态码
                int statusCode = response.getStatusLine().getStatusCode();
                System.out.println("Status Code: " + statusCode);

                // 获取响应头

                // 获取响应体
                String responseBody = EntityUtils.toString(response.getEntity());
                System.out.println("Response Body: " + responseBody);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static String getcoo() {

        try (CloseableHttpClient httpClient = HttpClients.custom()
                .setDefaultCookieStore(new BasicCookieStore())
                .setDefaultRequestConfig(RequestConfig.custom()
                        .setCookieSpec(CookieSpecs.STANDARD).build())
                .build()) {
            // 创建HttpPost请求
            HttpPost request = new HttpPost("https://www.jisilu.cn/webapi/account/login_process/");
            request.setHeader("Accept", "application/json, text/javascript, */*; q=0.01");
            request.setHeader("Accept-Encoding", "gzip, deflate, br, zstd");
            request.setHeader("Accept-Language", "zh-CN,zh;q=0.9");
            request.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
            request.setHeader("Origin", "https://www.jisilu.cn");
            request.setHeader("Priority", "u=1, i");
            request.setHeader("Referer", "https://www.jisilu.cn/account/login/");
            request.setHeader("Sec-Ch-Ua", "\"Google Chrome\";v=\"131\", \"Chromium\";v=\"131\", \"Not_A Brand\";v=\"24\"");
            request.setHeader("Sec-Ch-Ua-Mobile", "?0");
            request.setHeader("Sec-Ch-Ua-Platform", "\"Windows\"");
            request.setHeader("Sec-Fetch-Dest", "empty");
            request.setHeader("Sec-Fetch-Mode", "cors");
            request.setHeader("Sec-Fetch-Site", "same-origin");
            request.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36");
            request.setHeader("X-Requested-With", "XMLHttpRequest");
            // 设置请求体
            StringEntity params = new StringEntity(
                    "return_url=https%3A%2F%2Fwww.jisilu.cn%2F&" +
                            "user_name=778c501c45a77bf8b7a28850690c293d&" +
                            "password=f343f087960b997e56376edaf3db2ddf&" +
                            "auto_login=1&" +
                            "aes=1", "UTF-8");
            request.setEntity(params);

            // 发送请求
            try (CloseableHttpResponse response = httpClient.execute(request)) {
                // 获取响应状态码
                int statusCode = response.getStatusLine().getStatusCode();
                System.out.println("Status Code: " + statusCode);

                Header[] headers = response.getHeaders("set-cookie");
                for (Header header : headers) {
                    if (header.getValue().length() > 100) {
                        return header.getValue();

                    }
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


}
